// distributor/index/index.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    order_list: [],
    show: 1,
    page: 0,
    max_order_id: 0,
    longitude: 0,
    latitude: 0,
    maskhide: 0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (a) {
    getApp().pageOnLoad(this, a);
    var that = this;
    wx.getLocation({
      success: function (res) {
        that.setData({
          latitude: res.latitude,
          longitude: res.longitude
        },function () {
          that.getOrder()
        })
      },
      fail: function () {
        wx.showModal({
          title: '提示',
          content: '请开启定位后使用此功能',
          showCancel: false
        })
      }
    })
  },

  // 获取数据
  getOrder: function () {
    var that = this;
    getApp().request({
      url: getApp().api.distributor.get_order,
      data: {
          page: that.data.page,
          status: that.data.show,
          latitude: that.data.latitude,
          longitude: that.data.longitude
      },
      success: function(t) {
          if (t.code == 0) {
            var list = that.data.order_list.concat(t.data.list);
            if (that.data.show == 1) {
              that.setData({
                max_order_id: t.data.list.length>0 ? t.data.list[0].order_id : 0
              },function () {
                that.getNewOrder();
              })
            }
            that.setData({
              order_list: list,
              page:++that.data.page,
            })
          }
      }
    });
  },
  tabclick: function (t) {
    var index=t.target.dataset.index,that=this;;
    this.setData({
      show: index,
      page: 0,
      order_list: []
    },function () {
      that.getOrder()
    })
  },
  qiangdan: function (t) {
    console.log(t)
    var that = this,id=t.target.dataset.id,index=t.target.dataset.index;
    getApp().request({
      url: getApp().api.distributor.submit_order,
      data: {
          id: id
      },
      success: function(t) {
          wx.showToast({
            icon: 'none',
            title: t.msg,
          })
          if (t.code == 0) {
            var order_list = that.data.order_list;
            order_list.splice(index, 1); 
            that.setData({
              order_list: order_list
            })
          }
      }
    });
  },
  get_mch_d: function () {

  },
  daodian: function (t) {
    var that = this,id=t.target.dataset.id,index=t.target.dataset.index;
    getApp().request({
      url: getApp().api.distributor.daodian_order,
      data: {
          id: id
      },
      success: function(t) {
          wx.showToast({
            icon: 'none',
            title: t.msg,
          })
          if (t.code == 0) {
            var order_list = that.data.order_list;
            order_list.splice(index, 1); 
            that.setData({
              order_list: order_list
            })
          }
      }
    });
  },
  songda: function (t) {
    var that = this,id=t.target.dataset.id,index=t.target.dataset.index;
    getApp().request({
      url: getApp().api.distributor.songda_order,
      data: {
          id: id
      },
      success: function(t) {
          wx.showToast({
            icon: 'none',
            title: t.msg,
          })
          if (t.code == 0) {
            var order_list = that.data.order_list;
            order_list.splice(index, 1); 
            that.setData({
              order_list: order_list
            })
          }
      }
    });
  },
  getNewOrder: function () {
    var that = this;
    setInterval(() => {
      if (that.data.show == 1) {
        getApp().request({
          url: getApp().api.distributor.get_order,
          data: {
              latitude: that.data.latitude,
              longitude: that.data.longitude,
              max_order_id: that.data.max_order_id,
              status: 1
          },
          success: function(t) {
              if (t.code == 0) {
                var list = that.data.order_list.concat(t.data.list);
                if (t.data.list.length > 0) {
                  that.dingding()
                }
                if (that.data.show == 1) {
                  that.setData({
                    max_order_id: t.data.list.length>0 ? t.data.list[0].order_id : that.data.max_order_id
                  })
                  that.setData({
                    order_list: list,
                  })
                }
                
              }
          }
        });
      }
    }, 3000);
    
  },

  telShop: function (t) {
    var tel = t.target.dataset.tel
    wx.makePhoneCall({
      phoneNumber: tel
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    getApp().pageOnShow(this);
    wx.setKeepScreenOn({
      keepScreenOn: true
    })
  },
  onHide: function () {
    wx.setKeepScreenOn({
      keepScreenOn: false
    })
  },

  onUnload: function () {
    wx.setKeepScreenOn({
      keepScreenOn: false
    })
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    wx.redirectTo({
      url: '/distributor/index/index',
    })
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },
  dingding: function () {
    const answerRightAudio = wx.createInnerAudioContext();
    answerRightAudio.autoplay = false;
    answerRightAudio.src = this.data.__wxapp_img.wav.dingdong;
    // 答错音效
    const answerWrongAudio = wx.createInnerAudioContext();
    answerWrongAudio.autoplay = false;
    answerWrongAudio.src = this.data.__wxapp_img.wav.dingdong;
    answerRightAudio.play()
  },
  goUrl: function (t) {
    var url =t.currentTarget.dataset.url
    console.log(url,t)
    wx.navigateTo({
      url: url,
    })
  },
  maskclick: function () {
    var that = this;
    that.setData({
      maskhide: !that.data.maskhide
    })
  },
  toBack: function () {
    wx.navigateBack({
      delta: 0,
    })
  }

})